import Vue from 'vue'
import VueRouter from 'vue-router'
import routesConfig from '../js/config/routes-config'
import $N from 'nprogress'
import 'nprogress/nprogress.css'

Vue.use(VueRouter)

const router = new VueRouter({
    ...routesConfig
});

router.beforeEach((to, from, next) => {
    $N.start();
    // 判断是否存在路由
    if (to.matched.length === 0) {  //如果未匹配到路由
        $N.done();
        //如果上级也未匹配到路由则跳转404页面，如果上级能匹配到则转上级路由
        from.name ? next({ name: from.name }) : next({ path: '/error-404' });
    } else {
        //页面 title 动态修改 
        if (to.meta && to.meta.title) {
            document.title = to.meta.title;
        } else {
            document.title = 'Item';
        }
        next();
        // const token = $S.Get('token') || '';
        // $N.done();
        // if(!token && to.path !== '/login'){
        //     next({
        //         path: '/login'
        //     });
        // }else{
        //     if(to.path === '/'){
        //         next('/cos');
        //     }else{
        //         next();
        //     }

        // }        
    }
});

router.afterEach((to, from, next) => {
    $N.done();
});


const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return routerPush.call(this, location).catch(error => error)
}

export default router
